home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 June / SGI Freeware 1998 June.iso / dist / fw_UMINNgopher.idb / usr / freeware / src / gopher_1.12 / gopherd / ext.c.z / ext.c
C/C++ Source or Header  |  1997-09-09  |  5KB  |  221 lines

  1. /********************************************************************
  2.  * $Author: drich $
  3.  * $Revision: 1.1 $
  4.  * $Date: 1995/10/03 04:08:26 $
  5.  * $Source: /proj/freeware1.0/gopher1.12/src/gopherd/RCS/ext.c,v $
  6.  * $Status: $
  7.  *
  8.  * Paul Lindner, University of Minnesota CIS.
  9.  *
  10.  * Copyright 1991, 1992 by the Regents of the University of Minnesota
  11.  * see the file "Copyright" in the distribution for conditions of use.
  12.  *********************************************************************
  13.  * MODULE: ext.c
  14.  * These fns define mapping of file extensions to gopher objects.
  15.  *********************************************************************
  16.  * Revision History:
  17.  * $Log: ext.c,v $
  18.  * Revision 1.1  1995/10/03  04:08:26  drich
  19.  * gopher 1.2 check-in
  20.  *
  21.  * Revision 1.1  1992/12/10  23:13:27  lindner
  22.  * gopher 1.1 release
  23.  *
  24.  *
  25.  *********************************************************************/
  26.  
  27. #include "ext.h"
  28. #include "Malloc.h"
  29. #include <ctype.h>
  30.  
  31. extern boolean DEBUG;
  32.  
  33. /*
  34.  * Some functions to initialize and destroy sites and extensions..
  35.  * (Needed for use with DynArrays...)
  36.  */
  37.  
  38. static Ext *
  39. ExtNew()
  40. {
  41.      Ext *temp;
  42.  
  43.      temp = (Ext *) malloc(sizeof(Ext));
  44.      
  45.      if (temp == NULL)
  46.       return(NULL);
  47.  
  48.      temp->Objtype   = '\0';
  49.      temp->GplusType = STRnew();
  50.      STRinit(temp->GplusType);
  51.      temp->Prefix    = STRnew();
  52.      STRinit(temp->Prefix);
  53.      temp->ext       = STRnew();
  54.      STRinit(temp->ext);
  55.  
  56.      return(temp);
  57. }
  58.  
  59. static void ExtDestroy(ext)
  60.   Ext *ext;
  61. {
  62.      STRdestroy(ext->GplusType);
  63.      STRdestroy(ext->Prefix);
  64.      STRdestroy(ext->ext);
  65.      
  66.      free(ext);
  67. }
  68.  
  69. static void
  70. Extcpy(ext1, ext2)
  71.   Ext *ext1, *ext2;
  72. {
  73.      ext1->Objtype  = ext2->Objtype;
  74.      STRcpy(ext1->GplusType, ext2->GplusType);
  75.      STRcpy(ext1->Prefix, ext2->Prefix);
  76.      STRcpy(ext1->ext, ext2->ext);
  77. }
  78.  
  79. static void
  80. ExtSet(ext, objtype, gplustype, prefix, fileext)
  81.   Ext *ext;
  82.   char       objtype;
  83.   char       *gplustype;
  84.   char       *prefix;
  85.   char       *fileext;
  86. {
  87.      ext->Objtype = objtype;
  88.      STRset(ext->GplusType, gplustype);
  89.      STRset(ext->Prefix, prefix);
  90.      STRset(ext->ext, fileext);
  91. }
  92.  
  93. /******************************************************/
  94. /* Functions external routines can call.    */
  95.  
  96. ExtArray *
  97. ExtArrayNew()
  98. {
  99.      ExtArray *temp;
  100.      
  101.      temp = DAnew(20, ExtNew, NULL, ExtDestroy, Extcpy);
  102.      
  103.      return(temp);
  104. }
  105.  
  106. void
  107. ExtAdd(extarr, objtype, gplustype, prefix, fileext)
  108.   ExtArray *extarr;
  109.   char objtype;
  110.   char *gplustype;
  111.   char *prefix;
  112.   char *fileext;
  113. {
  114.      Ext *ext;
  115.  
  116.      if (DEBUG) printf("Adding extension '%s', objtype %c, prefix %s, gplustype '%s'\n",
  117.                fileext, objtype, prefix, gplustype);
  118.      
  119.      ext = ExtNew();
  120.      
  121.      ExtSet(ext, objtype, gplustype, prefix, fileext);
  122.      
  123.      DApush(extarr, ext);
  124.      
  125.      ExtDestroy(ext);
  126.      
  127. }
  128.  
  129. /*
  130.  * Get the parameters associated with a particular extension
  131.  */
  132.  
  133. void
  134. ExtGet(extarr, fileext, objtype, gplustype, prefix)
  135.   ExtArray *extarr;
  136.   char *fileext;
  137.   char *objtype;
  138.   char **gplustype;
  139.   char **prefix;
  140. {
  141.      int i;
  142.      Ext *temp;
  143.  
  144.      *objtype   = '\0';
  145.      *gplustype = NULL;
  146.      *prefix    = NULL;
  147.  
  148.      /*** Linear search.  Ick. ***/
  149.      
  150.      for (i=0; i< DAgetTop(extarr); i++) {
  151.  
  152.       temp = ExtgetEntry(extarr,i);
  153.       
  154.       if (strcasecmp(fileext+strlen(fileext)-strlen(STRget(temp->ext)), STRget(temp->ext))==0) {
  155.  
  156.            *objtype   = temp->Objtype;
  157.            *gplustype = STRget(temp->GplusType);
  158.            *prefix    = STRget(temp->Prefix);
  159.            
  160.            return;
  161.       }
  162.      }
  163. }
  164.  
  165. boolean 
  166. ExtProcessLine(extarr, inputline)
  167.   ExtArray *extarr;
  168.   char     *inputline;
  169. {
  170.      int i;
  171.      char ext[64];
  172.      char Gtype;
  173.      char prefix[64];
  174.      char Gplustype[64];
  175.  
  176.      /** Strip out the white space **/
  177.      while (isspace(*inputline))
  178.       inputline++;
  179.      if (*inputline == '\0') return(FALSE);
  180.  
  181.      /*** The extension itself ***/
  182.      i=0;
  183.      while (!isspace(*inputline))
  184.       ext[i++] = *inputline++;
  185.      if (*inputline == '\0') return(FALSE);
  186.  
  187.      ext[i] = '\0';
  188.      
  189.      while (isspace(*inputline))
  190.       inputline++;
  191.      if (*inputline == '\0') return(FALSE);
  192.  
  193.      /*** The Gophertype ***/
  194.      Gtype = *inputline++;
  195.      
  196.      while (isspace(*inputline))
  197.       inputline++;
  198.      if (*inputline == '\0') return(FALSE);
  199.  
  200.      /*** Prefix ***/
  201.      i=0;
  202.      while (!isspace(*inputline))
  203.       prefix[i++] = *inputline++;
  204.      if (*inputline == '\0') return(FALSE);
  205.      prefix[i]='\0';
  206.  
  207.      while (isspace(*inputline))
  208.       inputline++;
  209.      if (*inputline == '\0') return(FALSE);
  210.  
  211.      /*** Gopher + type ***/
  212.      i=0;
  213.      while (!isspace(*inputline))
  214.       Gplustype[i++]= *inputline++;
  215.  
  216.      Gplustype[i] = '\0';
  217.  
  218.      ExtAdd(extarr, Gtype, Gplustype, prefix, ext);
  219.      return(TRUE);
  220. }
  221.